package com.engine.cese.duss.sms.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.cese.duss.sms.util.UPMutil;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
*@Description: 红豆电信发送短信
*@Author: Dusongsong
*@Date: 2020/11/17
*/
public class WeaSendSMSAction {
    /**
     * 发送短信
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/sendSMS")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSendSMS(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        BaseBean baseBean = new BaseBean();
        String phone = Util.null2String( request.getParameter( "phone" ) );
        String content = Util.null2String( request.getParameter( "content" ) );
        String massURL = baseBean.getPropValue( "hodo","url");
        String username =  baseBean.getPropValue( "hodo","username");
        String password =  baseBean.getPropValue( "hodo","password");
        String param = UPMutil.getMassJsonContent( phone, content );
        Map<String, String> headers = new HashMap<>();
        String authorization = UPMutil.generateAuthorization( username, password );
        headers.put( "accept", "application/json" );
        headers.put( "Content-Type", "application/json; charset=utf-8" );
        headers.put( "Authorization", authorization );
        String encode = "UTF-8";
        try {
            String result = UPMutil.httpPostRaw( massURL, param, headers, encode );
            RecordSet rs = new RecordSet();
            JSONObject resultJson = JSONObject.parseObject( result );
            String code = resultJson.getString( "code" );
            if ("0".equals( code )) {
                String status = "0";
                String curtime = DateUtil.getFullDate();
                UUID MIT_UUID = UUID.randomUUID();
                String uuid = MIT_UUID.toString(); //唯一标志uuid
                String sql = "insert into uf_smscode (uuid,smscode,mobile,status,systemdate) values (?,?,?,?,?)";
                rs.executeUpdate( sql, new Object[]{uuid, content,phone,status, curtime} );
                apidatas.put("api_status", true);
            }else {
                apidatas.put("api_status", result);
            }

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("error_message", "发送短信请求异常");
        }
        return JSONObject.toJSONString(apidatas);
    }


}
